package com.kelaite.procurement;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.kelaite.procurement.interceptor.UserLoginInterceptor;

/*@Configuration
 @ComponentScan
 @EnableJpaRepositories
 @Import({RepositoryRestMvcConfiguration.class
 ,MvcConfig.class
 //,SecurityConfig.class
 })
 */
@SpringBootApplication
@PropertySources({ @PropertySource("classpath:build_version.properties"),
		@PropertySource("classpath:application.properties"),
		@PropertySource("classpath:application_${app.env}.properties") })
// @ServletComponentScan
public class Application extends WebMvcConfigurerAdapter {

	public static void main(String[] args) {
		/**
		 * by default, we adopt local configuration in PROD env, we will setup
		 * app.env with -D option while running the Application.
		 */

		String env = System.getProperty("app.env");
		if (env == null || env.equals("")) {
			System.setProperty("app.env", "local");
		}

		System.setProperty("file.encoding", "UTF-8");
		SpringApplication.run(Application.class, args);
	}

	/**
	 * 配置拦截器
	 * 
	 * @param registry
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new UserLoginInterceptor())
				.addPathPatterns("/supplier/**")
				.addPathPatterns("/estimate/**").addPathPatterns("/message/**")
				.addPathPatterns("/user/**").addPathPatterns("/pay/**")
				.addPathPatterns("/client/**").addPathPatterns("/maintance/**");
		registry.addInterceptor(localeChangeInterceptor());
	}

	@Bean
	public LocaleChangeInterceptor localeChangeInterceptor() {
		LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
		localeChangeInterceptor.setParamName("lang");
		return localeChangeInterceptor;
	}

	@Bean(name = "localeResolver")
	public LocaleResolver sessionLocaleResolver() {
		SessionLocaleResolver localeResolver = new SessionLocaleResolver();
//		localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
		return localeResolver;
	}    
	
	@Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:i18n/messages");
        messageSource.setCacheSeconds(3600); //refresh cache once per hour
        return messageSource;
	}
}